<template>
  <!-- 单位类型配置-->
  <div class="app-container">
    <div class="form-container">
      <el-form
        :inline="true"
        label-width="80px"
        :model="filters"
        class="selectFrom"
      >
        <el-form-item label="类型名称" prop="typeName">
          <el-input
            v-model="filters.typeName"
            size="small"
            placeholder="请输入类型名称"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <el-button v-clickForbidden size="middle" icon="el-icon-search" @click="handleSearch()">搜索</el-button>
      <el-button v-clickForbidden size="middle" icon="el-icon-refresh" @click="handleReset()">重置</el-button>
      <el-button v-hasPermi="['supervise:certificateTemplate:list']" size="middle" icon="el-icon-plus" :disabled="single" @click="AddCertTemplate()">配置注册信息</el-button>
      <el-button v-hasPermi="['project:supperType:selectTempId']" size="middle" icon="el-icon-plus" :disabled="single" @click="baseTemp">基础套餐</el-button>
    </div>
    <!-- 表格展示start -->
    <div ref="table">
      <el-table
        ref="multipleTable"
        v-loading="tableLoading"
        class="table-list"
        stripe
        :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height-tree' : 'el-body-page-roll-height-tree') :
          (needTagsView===true ? 'el-body-page-roll-tag-tree' : 'el-body-page-roll-tree')"
        :data="treeData"
        :row-style="rowClass"
        row-key="id"
        :default-expand-all="false"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @selection-change="handleSelectionChange"
        @row-click="rowChecked"
      >
        <el-table-column type="selection" width="38" align="center" />
        <el-table-column prop="typeName" label="类型名称" min-width="180" header-align="center" align="left" />
        <el-table-column prop="typeNum" label="类型编号" min-width="40" align="center" />
        <el-table-column prop="tempName" label="基础套餐" min-width="120" align="center" />
        <el-table-column prop="orderNum" label="排序" min-width="40" align="center" />
        <el-table-column prop="remark" :show-overflow-tooltip="true" label="备注" min-width="180" align="center" />
      </el-table>
    </div>
    <!-- 表格展示end-->
    <baseTemp ref="baseTemp" />
  </div>
</template>

<script>
import baseTemp from './components/baseTemp'
import { listOrganization, getTempType } from '@/api/superviseBaseInfo/typeManage/organizationType'
import { mapState } from 'vuex'
export default {
  name: 'OrganizationType',
  components: {
    baseTemp
  },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      },
      // 启用/停用显示状态
      disabledStatus: '0',
      disabledContent: '停用',
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      treeData: [],
      // 套餐列表
      tempList: []
      /* 新增 end */
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    this.getList()
    this.getTempType()
  },
  methods: {
    // 获取套餐类型
    async getTempType() {
      const { data: { rows }} = await getTempType({ isFixed: '0' })
      this.tempList = rows
    },
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows }} = await listOrganization(this.filters)
        this.total = rows.length || 0
        this.treeData = rows
        this.tableLoading = false
        // this.AddTaleData(rows, this.total)
      } catch {
        this.tableLoading = false
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else { // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
      } else {
        this.multipleSelection = selection
        // eslint-disable-next-line no-prototype-builtins
        if (this.multipleSelection.length === 1 && this.multipleSelection[0].hasOwnProperty('roleId')) {
          this.disabledStatus = this.multipleSelection[0].status
          if (this.disabledStatus === '0') {
            this.disabledContent = '停用'
          } else {
            this.disabledContent = '启用'
          }
        }
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.selectRow = []
        if (selection.length > 0) {
          selection.forEach((item, index) => {
            this.selectRow.push(this.treeData.indexOf(item))
          })
        }
        // 如果选择的数组大于1
        if (selection.length > 1) {
          // 清空所有的选中
          this.$refs.multipleTable.clearSelection()
          // 将数组的最后一个选中
          this.$refs.multipleTable.toggleRowSelection(selection.pop())
        }
      }
    },
    // 空白行添加
    AddTaleData(arr, num) {
      const tableHeight = this.$refs.table.offsetHeight
      const row = Math.floor(
        (tableHeight - 36) / 36 // 表格：表头/行高：36px
      )
      for (let i = 0; i < (row - num); i++) {
        this.treeData.push({})
      }
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置搜索条件
    handleReset() {
      this.filters = {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      }
      this.getList()
    },
    // 新增证件类型模板
    AddCertTemplate() {
      // 获取选中行的id
      const id = this.multipleSelection[0].id
      this.$router.push({
        path: '/systemConfig/certTypeTemplate',
        query: { id }
      })
    },
    // 基本套餐
    baseTemp() {
      this.$refs.baseTemp.handleAdd(this.multipleSelection[0].id, this.tempList)
    }
  }
}
</script>

